package leetcode.median;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

import leetcode.base.TreeNode;

public class $515_LargestValues {

    List<Integer> res = new LinkedList<>();

    public List<Integer> largestValues(TreeNode root) {
        if (root == null) {
            return res;
        }
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.offerLast(root);
        while (!deque.isEmpty()) {
            int len = deque.size();
            int max = Integer.MIN_VALUE;
            for (int i = 0;i < len;i++) {
                TreeNode treeNode = deque.pollFirst();
                max = Math.max(treeNode.val, max);
                if (treeNode.left != null) {
                    deque.offerLast(treeNode.left);
                }
                if (treeNode.right != null) {
                    deque.offerLast(treeNode.right);
                }
            }
            res.add(max);
        }
        return res;
    }
}
